package com.cykj.service.impl;

import com.cykj.mapper.DetailsMapper;
import com.cykj.pojo.CyDetails;
import com.cykj.service.DetailsService;
import com.cykj.util.PageResult;
import com.cykj.util.PageUtils;
import com.cykj.util.ResponseDto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DetailsServiceImpl implements DetailsService {
    @Autowired
    private DetailsMapper detailsMapper;
    @Override
    public ResponseDto getAllDetail(int pageCurr, int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyDetails> allDetail = detailsMapper.getAllDetail();
        if (allDetail != null){
            PageInfo<Object> pageInfo = new PageInfo<>(page);
            PageResult pageResult = PageUtils.getPageResult(pageInfo);
            return new ResponseDto(1,"查询成功",pageResult);
        }else {
            return new ResponseDto(0,"查询失败",null);
        }
    }
    @Override
    public ResponseDto changeStatus(int proStatus, int detailsId) {
        int i = detailsMapper.changeStatus(proStatus, detailsId);
        if (i != 0){
            return new ResponseDto(1,"修改成功",i);
        }else {
            return new ResponseDto(0,"修改失败",null);
        }
    }

    @Override
    public ResponseDto deleteDetail(int detailsId) {
        int includeProject = detailsMapper.isIncludeProject(detailsId);
        if (includeProject != 0 ){
            return new ResponseDto(-1,"项目包含该细项，无法删除",null);
        }
        int i = detailsMapper.deleteDetail(detailsId);
        if (i != 0){
            return new ResponseDto(1,"删除成功",null);
        }else {
            return new ResponseDto(0,"删除失败",null);
        }
    }

    @Override
    public ResponseDto alterDepart(int detailsId, String detailsName, String detailsUnit, String detailsUp, String detailsDown) {
        CyDetails cyDetails = detailsMapper.searchAlterDetails(detailsId, detailsName, detailsUnit, detailsUp, detailsDown);
        if (cyDetails != null){
            return new ResponseDto(2,"修改失败",null);
        }else {
            int i = detailsMapper.alterDetails(detailsId, detailsName, detailsUnit, detailsUp, detailsDown);
            if (i != 0){
                return new ResponseDto(1,"修改成功",null);
            }else {
                return new ResponseDto(0,"修改失败",null);
            }
        }
    }

    @Override
    public ResponseDto searchDetailsInfo(String value, int pageCurr, int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyDetails> cyDetails = detailsMapper.searchDetailsInfo(value);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1,"查询成功",pageResult);
    }

    @Override
    public ResponseDto getAllDetailsName() {
        List<String> allDetailsName = detailsMapper.getAllDetailsName();
        return new ResponseDto(1,"查询成功",allDetailsName);
    }

    @Transactional(rollbackFor = Exception.class)//如果出现异常了直接回滚
    @Override
    public ResponseDto commitDelete(String checkedDetails) {
        //前端将要删除的细项名称变为字符串，再进行分隔
        String[] split = checkedDetails.split("，");
        int length = split.length;
        int count = 0;

        try {
            for (int i = 0; i < length; i++){
                int includeProject = detailsMapper.isIncludeProjectName(split[i]);
                if (includeProject != 0 ){
                    return new ResponseDto(-1,"项目包含该细项，无法删除",null);
                }
            }
            for (int i = 0; i < length; i++) {
                int res = detailsMapper.commitDelete(split[i]);
                if (res >= 1) {
                    count++;
                }
            }
            if (count == length) {
                return new ResponseDto(1, "删除成功", null);
            } else {
                return new ResponseDto(0, "删除失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public ResponseDto fatherDepartIdIsExist(String fatherDepartId) {
        try {
            int departId = Integer.parseInt(fatherDepartId);
            int i = detailsMapper.fatherDepartIdIsExist(departId);
            if (i != 0){
                return new ResponseDto(1,"科室存在",i);
            }else {
                return new ResponseDto(0,"科室不存在",null);
            }
        }catch (Exception e){
            return new ResponseDto(-1,"添加细项输入类型错误",null);
        }
    }

    @Override
    public ResponseDto addDetailCommit(String detailName, String fatherDepartId,
                                       String detailUnit, String detailUp, String detailDown, String status) {
        if (status.equals("2")){
            status = "0";
        }
        int i = detailsMapper.addDetailCommit(
                detailName, Integer.parseInt(fatherDepartId), detailUnit,
                detailUp, detailDown, Integer.parseInt(status));
        return new ResponseDto(1, "插入成功", null);
    }
}
